(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.custPay.updateController", updateController);
	updateController.$inject = [ "$scope", "$q", "modalService", "layoutConfig", "validatorConfig", "authService", "custPayService" ];
	function updateController($scope, $q, modalService, layoutConfig, validatorConfig, authService, custPayService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.custPay = {};

		ctrl.init = function() {
			custPayService.initDataOptions().then(function(data) {
				return $q(function(resolved, rejected) {
					ctrl.payCcyOptions = data.currency;
					ctrl.countryOptions = data.country;
					ctrl.communityIdOptions = data.communityId;
					resolved();
				});
			}).then(function() {
				return $q(function(resolved, rejected) {
					authService.updateInit(params).then(function(response) {
						if (response.data && response.data.model && response.data.task) {
							var custPay = response.data.model;
							custPay.taskId = response.data.task.taskId;
							resolved(custPay);
						}
					});
				});
			}).then(function(custPay) {
				custPayService.getLayout("custPay", "update", custPay.prodType, false).then(function(response) {
					ctrl.layoutConfig = response || layoutConfig;
					ctrl.custPay = custPay;
				});
			});
		};

		function getCurrencyOptions() {
			var defer = $q.defer();
			ajaxServices.getCurrencyOptions().then(function(response) {
				defer.resolve(response ? response.data : []);
			}, angular.noop);
			return defer.promise;
		}
		
		ctrl.communityIdChanged = function(custPay){
			if(custPay.communityId){
				custPayService.getApCommunity(custPay.communityId).then(function(map){
					if(map && map.company){
						custPay.payerCode = map.company.companyCode;
						custPay.payerName = map.company.companyName;
						custPay.payerAddr1 = map.company.companyAddr1;
						custPay.payerAddr2 = map.company.companyAddr2;
						custPay.payerAddr3 = map.company.companyAddr3;
						custPay.payerAddr4 = map.company.companyAddr4;
						custPay.payerCity = map.company.companyCity;
						custPay.payerZip = map.company.companyZip;
						custPay.payerState = map.company.companyState;
						custPay.payerCountry = map.company.companyCountry;
					}
				});
			}
		};

		ctrl.save = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				return authService.updateSaved(ctrl.custPay).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.settlement = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				return authService.updateSubmitted(ctrl.custPay).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.cancel = function() {
			modalService.openConfirmDialogs({
				"messages" : [ {
					msg : "title.dialog.confirm.cancel",
					args : [ ctrl.custPay.primaryKey ]
				} ],
				"btnCallback" : {
					"ok" : function() {
						authService.updateCancelled(ctrl.custPay).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.cancel" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

	}
})(angular);
